package it.cst.entity.dao;

import it.cst.entity.Group;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class GroupDAOImpl implements GroupDAO{
	@Autowired
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Integer addGroup(Group group) {
		sessionFactory.getCurrentSession().save(group);
		return group.getId();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Group> listGroup() {
		return sessionFactory.getCurrentSession().createQuery("from Group").list();
	}

	@Override
	public void removeGroup(Group g) {
		sessionFactory.getCurrentSession().delete(g);
	}

	@Override
	public Group getGroup(Integer id) {
		Group g = (Group) sessionFactory.getCurrentSession().get(Group.class, id);
		return g;		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Group> listGroup(Integer idMatch) {
		Query q = sessionFactory.getCurrentSession().createQuery("from Group WHERE idMatch = ?");
		q.setInteger(0, idMatch);
		return q.list();
	}

}
